package com.xy.controller.calculate.maori;

import com.xy.api.model.dto.calculate.maori.MaoriProductInstanceDTO;
import com.xy.api.model.request.calculate.maori.MaoriProductRequest;
import com.xy.api.model.request.calculate.maori.SubmitMaoriProductInstanceRequest;
import com.xy.api.model.response.R;
import com.xy.api.model.response.Rows;
import com.xy.api.service.calculate.maori.IMaoriProductInstanceService;
import com.xy.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/calculate/maori/product/instance")
public class MaoriProductInstanceController extends BaseController {

    @Autowired
    private IMaoriProductInstanceService maoriProductInstanceService;

    @PostMapping("/list")
    public R queryProductInstanceList(@RequestBody MaoriProductRequest request) {
        List<MaoriProductInstanceDTO> list = maoriProductInstanceService.queryProductInstanceList(request.getId());
        Rows<MaoriProductInstanceDTO> rows = new Rows<>();
        rows.setRows(list);
        rows.setTotal(list.size());
        return success(rows);
    }

    @PostMapping("/totalInstance")
    public R queryTotalInstance(@RequestBody MaoriProductRequest request) {
        MaoriProductInstanceDTO instance = maoriProductInstanceService.queryTotalInstance(request.getId());
        return success(instance);
    }

    @PostMapping("/addInstance")
    public R addInstance(@RequestBody MaoriProductInstanceDTO request) {
        MaoriProductInstanceDTO instance = maoriProductInstanceService.addInstance(request);
        return success(instance);
    }

    @PostMapping("/save")
    public R saveProductInstance(@RequestBody SubmitMaoriProductInstanceRequest request) {
        maoriProductInstanceService.saveProductInstance(request);
        return success();
    }

    @PostMapping("/delete")
    public R deleteProductInstance(@RequestBody SubmitMaoriProductInstanceRequest request) {
        maoriProductInstanceService.deleteProductInstance(request);
        return success();
    }

    @PostMapping("/calculate")
    public R calculateInstance(@RequestBody SubmitMaoriProductInstanceRequest request) {
        maoriProductInstanceService.saveProductInstance(request);
        maoriProductInstanceService.calculate(request.getCostingProductId());
        return success();
    }
}
